#include "client.h"

void * client_handler(void * parameters) {

    char buffer[512];
    char name[50];
    int action = -1, subAction = -1;
    int i, current_player, req_player;

    struct lobby * clients = (struct lobby*)parameters;

    current_player = find_id_again(clients);
    printf("Debug:\nId: %d \nSocket: %d\nThread: %d\n", 
current_player, clients[current_player].player_socket, pthread_self());
    printf("Servern väntar på klient %d att svara\n", current_player);
    while(recv(clients[current_player].player_socket, buffer, sizeof(buffer), 0) > 0) {

        sscanf(buffer, "%d", &action);

        if(action == 0) {

            sscanf(buffer, "%d %s", &action, &name);
            if(check_name(name, clients)) {
		
		        printf("Namnet finns redan\n");
                sprintf(buffer, "%d %d\n", 0, FALSE);
            } else {

                sprintf(buffer, "%d %d\n", 0, TRUE);
                strcpy(clients[current_player].player_name, name);
                printf("spelare %d heter nu %s\n", current_player, clients[current_player].player_name);
            }

            send(clients[current_player].player_socket, buffer, strlen(buffer) + 1, 0);

        } else if(action == 1) {

            sscanf(buffer, "%d %d", &action, &req_player);
            if(action) {

                if(clients[req_player].player_id >= 0) {

                    sprintf(buffer, "%d %d %s %s %d", 1, clients[req_player].player_id, clients[req_player].player_name, clients[req_player].player_ip, clients[req_player].player_port);
                } else {

                    sprintf(buffer, "%d %d", 1, -1);
                }
                send(clients[current_player].player_socket, buffer, strlen(buffer) + 1, 0);
            }
           
        } else if(action == 2) {

            sscanf(buffer, "%d %d %d %d", &action, &(clients[current_player].challenge_player_id), &clients[current_player].map, &clients[current_player].character);
            clients[clients[current_player].challenge_player_id].challenge_player_id = current_player;
            printf("spelare %d vill utmana %d på map %d med char %d\n", current_player, clients[current_player].challenge_player_id, clients[current_player].map, clients[current_player].character);
            sprintf(buffer, "%d %d %d %d", 2, current_player, clients[current_player].map, clients[current_player].character);
            send(clients[clients[current_player].challenge_player_id].player_socket, buffer, strlen(buffer) + 1, 0);

        } else if(action == 3) {

            sscanf(buffer, "%d %d %d", &action, &i, &clients[current_player].character);
            if(i) {

                printf("spelare %d accepterade spelet från spelare %d med char %d\n", current_player, clients[clients[current_player].challenge_player_id].player_id, clients[current_player].character);
                sprintf(buffer, "%d %d %d", 3, 1, clients[current_player].character);
            } else {

                printf("spelare %d accepterade inte spelet från spelare %d\n", current_player, clients[clients[current_player].challenge_player_id].player_id);
                sprintf(buffer, "%d %d", 3, 0);
            }

            send(clients[clients[current_player].challenge_player_id].player_socket, buffer, strlen(buffer) + 1, 0);

        } else if(action == 10) {

            sprintf(buffer, "%d", get_online_players(clients));
            send(clients[current_player].player_socket, buffer, strlen(buffer) + 1, 0);
        } else {

            break;
        }
    }
    printf("klient %d lämnade\n", clients[current_player].player_id);    
    clients[current_player].player_id = -1;
    clients[current_player].player_name[0] = 0;
    pthread_cancel(clients[current_player].player_thread);
    close(clients[current_player].player_socket);
    
    return 0;
}

int check_name(char *name, struct lobby *clients) {

    int i; 

    for(i = 0; i < MAXPLAYERS; i++) {

        if(strcmp(name, clients[i].player_name) == 0) {

            return 1;
        }
    }

    return 0;
}


/*
void send_lobby2(struct lobby *clients, int player_id) {

    int i;
    char buffer[512];
    for(i = 0; i < MAXPLAYERS; i++) {

    	if(clients[i].player_id != -1) {

            snprintf(buffer, sizeof(buffer), "%d %d %s %s %d\n", 1, clients[i].player_id, clients[i].player_name, clients[i].player_ip, clients[i].player_port);
            send(clients[player_id].player_socket, buffer, strlen(buffer) + 1, 0);
    	}   
    } 
}*/


void challenge_player(struct lobby *clients, int player_id, int opponent_id) {

    char buffer[512];
    int action, answer;

    printf("spelare %d vill utmana %d\n", player_id, opponent_id);

    sprintf(buffer, "%d %d %d\n", 2, player_id, opponent_id);
    send(clients[opponent_id].player_socket, buffer, strlen(buffer) + 1, 0);

    printf("spelare %d väntar på svar...\n", player_id);
    recv(clients[opponent_id].player_socket, buffer, sizeof(buffer), 0);
    sscanf(buffer, "%d %d", &action, &answer);

    if(action == 2 && answer == 1) {

        sprintf(buffer, "%d %d %s %d\n", action, player_id, clients[opponent_id].player_ip, clients[opponent_id].player_port);
        send(clients[opponent_id].player_socket, buffer, strlen(buffer) + 1, 0);
    } else {

        //inte match
    }
}

int find_id_again(struct lobby *clients) {

    int i;
    int thread_id = pthread_self();

    for(i = 0; i < MAXPLAYERS; i++) {

        if((int)clients[i].player_thread == thread_id) {

            return i;
        }
    }

    return -1;
}

int get_online_players(struct lobby *clients) {

    int i, online_players = 0; 
    for(i = 0; i < MAXPLAYERS; i++) {

        if(clients[i].player_id != -1) {

            online_players++;
        }
    }    

    return online_players;
}
